import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { EMPTY, throwError, Observable } from 'rxjs';
import { LetterDigestVo } from '../model/letter-digest-vo';
import { EntityPageResult } from '../model/entity-page-result';
import { LetterInboxVo } from '../model/letter-inbox-vo';
import { LetterInboxNoticeVo } from '../model/letter-inbox-notice-vo';
@Injectable({
  providedIn: 'root'
})
export class LetterService {
  private restRequestURI: string = environment.restURI + '/letter';
  /**
   * 消息REST API服务
   */
  constructor(private http: HttpClient) { }

  /**
   * 向指定的会员发送消息
   * @param memberId 发件人/会员ID
   * @param memberNickname 发件人的会员昵称
   * @param subject 消息的主题
   * @param message 消息的内容
   * @param receiver 收件人/会员ID
   * @returns 成功返回200(响应中有实体),失败404
   */
  public send(memberId: number, memberNickname: string | null, subject: string | null, message: string, receiver: number): Observable<LetterDigestVo> {
    if (0 >= memberId) {
      return throwError('发件人非可用值');
    }
    if (0 >= receiver) {
      return throwError('收件人非可用值');
    }
    if (!message || message.length == 0) {
      return throwError('消息内容非可用值');
    }
    let mn: string = memberNickname ?? "Member@u" + memberId;
    //:/sent
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams().set('sender', memberId + '').set('nickname', mn).set('subject', subject ?? mn + '发来一封私信').set('message', message).set('receiver', receiver + '');
    return this.http.post<LetterDigestVo>(this.restRequestURI + '/sent', body.toString(), httpOptions);
  }
  /**
   * 将会员收件箱中的未读消息标记为已读
   * 标记会员收件箱中的信件为已读
   * @param memberId 当前操作者/会员ID
   * @param direction 方向性标志(otherMemberId=(direction == 1)?memberId:-1)
   * @param otherMemberId 与方向性有关的另一个会员ID(会员收件箱中发件人,会员是收件人)
   * @returns 成功返回200,失败417
   */
  public readAll(memberId: number, direction: number, otherMemberId: number): Observable<string> {
    if (0 >= memberId) {
      return throwError('会员ID非可用值');
    }
    if (0 > otherMemberId && direction == 1) {
      return throwError('其它会员ID非可用值');
    }
    let omid: number = otherMemberId
    if (direction !== 1) {
      omid = -1;
    }
    //:/inbox/{member}/readed/all
    const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    const body = new HttpParams().set('target', omid + '').set('direction', direction + '');
    return this.http.put(this.restRequestURI + '/inbox/' + memberId + '/readed/all', body.toString(), { headers, "responseType": "text" });
  }
  /**
   * [批量]标记会员收件箱中的信件为已读
   * @param memberId 收件人/会员ID
   * @param messageIds 信件ID数组,格式:id1,id2,...idN
   * @return 成功返回200,失败417
   */
  public readed(memberId: number, messageIds: number[]): Observable<string> {
    if (0 >= memberId) {
      return throwError('会员ID非可用值');
    }
    if (messageIds.length == 0) {
      return throwError('请选择消息');
    }
    //:/inbox/{member}/readed
    const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
    const body = new HttpParams().set('records', messageIds.join());
    return this.http.put(this.restRequestURI + '/inbox/' + memberId + '/readed', body.toString(), { headers, "responseType": "text" });
  }
  /**
   * 消息聊天界面的数据源
   * @param sender 发件人/会员ID
   * @param receiver 收件人/会员ID
   * @param page 
   * @param pageSize 
   */
  public getMessageChat(sender: number, receiver: number, page: number = 1, pageSize: number = 20): Observable<EntityPageResult<LetterDigestVo>> {
    if (0 > sender) {
      return throwError('发件人非可用值');
    }
    if (0 >= receiver) {
      return throwError('收件人非可用值');
    }
    //:/chat/{member}/{sender}/{page}/{number}
    return this.http.get<EntityPageResult<LetterDigestVo>>(this.restRequestURI + '/chat/' + receiver + '/' + sender + '/' + page + '/' + pageSize);
  }
  /**
   * 查看指定会员的收件箱
   * @param memberId 收件人/会员ID
   * @param page 
   * @param pageSize 
   */
  public getInbox(memberId: number, page: number = 1, pageSize: number = 20): Observable<EntityPageResult<LetterInboxVo>> {
    if (0 >= memberId) {
      return throwError('收件人非可用值');
    }
    //:/inbox/{member}/{page}/{number}
    return this.http.get<EntityPageResult<LetterInboxVo>>(this.restRequestURI + '/inbox/' + memberId + '/' + page + '/' + pageSize);
  }
  /**
   * 查看收件箱中未读消息的数量
   * @deprecated
   * @param memberId 收件人/会员ID
   * @param senderIds 发件人/会员ID数组,格式:id1,id2,...idN
   * @returns {发件人1:数量, 发件人2:数量, ...}
   */
  public getUnreadSize(memberId: number, senderIds: number[]): Observable<any> {
    if (0 >= memberId) {
      return throwError('收件人非可用值');
    }
    if (senderIds.length == 0) {
      return throwError('缺少发件人ID');
    }
    //:/inbox/{member}/unreaded
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams().set('records', senderIds.join());
    return this.http.post(this.restRequestURI + '/inbox/' + memberId + '/unreaded', body.toString(), httpOptions);
  }
  /**
   * 查看收件箱中未读消息的数量
   * 
   * @param memberId 收件人/会员ID
   * @param senderIds 发件人/会员ID数组,格式:id1,id2,...idN
   */
  public getUnreadCollectSize(memberId: number, senderIds: number[]): Observable<LetterInboxNoticeVo[]> {
    if (0 >= memberId) {
      return throwError('收件人非可用值');
    }
    if (senderIds.length == 0) {
      return throwError('缺少发件人ID');
    }
    //:/inbox/{member}/unreaded
    const httpOptions = {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    };
    const body = new HttpParams().set('records', senderIds.join());
    return this.http.post<LetterInboxNoticeVo[]>(this.restRequestURI + '/inbox/' + memberId + '/unreaded/collect', body.toString(), httpOptions);
  }
  /**
   * 收件箱中未读的消息数量
   * @param memberId 收件人/会员ID
   */
  public getUnreadMessageCounter(memberId: number): Observable<string> {
    if (0 >= memberId) {
      return throwError('收件人非可用值');
    }
    //:/inbox/{member}/unreaded/size
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    return this.http.get(this.restRequestURI + '/inbox/' + memberId + '/unreaded/size', { headers, responseType: 'text' });
  }
  /**
   * 收件箱中指定发件人发来的未读的消息数量
   * @param memberId 收件人/会员ID
   * @param sender 发件人/会员ID
   */
  public getUnreadSenderMessageCounter(memberId: number, sender: number): Observable<string> {
    if (0 >= memberId) {
      return throwError('收件人非可用值');
    }
    //:/inbox/{member}/sender/{sender}/unreaded/size
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    return this.http.get(this.restRequestURI + '/inbox/' + memberId + '/sender/' + sender + '/unreaded/size', { headers, responseType: 'text' });
  }
}
